package cn.edu.zjicm.wordsnet_d.net;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import android.os.Handler;

public class Client {
	private Handler handler;

	public static String BASE_URL = "http://update.iwordnet.com/";
	public static String KEY = "newsclan2008";
	
	public static String BASE_VOICE_URL = "http://cdnx.iwordnet.com/";		
	/*
	 * public static String[] BASE_URL_LIST = new String[] {
	 * "http://iwordnet.com/nsp/geturl.php?path=",
	 * "http://update.iwordnet.com/", "http://wordsnet.sinaapp.com/android/"};
	 * //,"http://adc.zjicm.edu.cn/" };
	 */
//	public static String[] BASE_URL_LIST = {"http://update.iwordnet.com/"};
	
//	public static String[] BASE_URL_LIST;		
//
//	public Client(Handler handler) {
//		this.handler = handler;
//		new DownloadAddress().start();
//	}
//
//	public class DownloadAddress extends Thread {
//
//		@Override
//		public void run() {
//			// TODO Auto-generated method stub
//			XmlPullParser webAddress = null;
//			URL xmlUrl;
//
//			try {
//				// 获取下载地址的网址
//				xmlUrl = new URL(BASE_URL + "voice.xml");
//				webAddress = XmlPullParserFactory.newInstance().newPullParser();
//				webAddress.setInput(xmlUrl.openStream(), null);
//
//				int eventType = -1;
//				while (eventType != XmlResourceParser.END_DOCUMENT) {
//					if (eventType == XmlResourceParser.START_TAG) {
//						String strName = webAddress.getName();
//						if (strName.equals("web")) {
//							String webAddressStr = webAddress
//									.getAttributeValue(null, "web_address");
//							BASE_URL_LIST = webAddressStr.split("#");
//							handler.sendMessage(handler
//									.obtainMessage(DownloadManager.MSG_GOT_DOWNLOAD_ADDRESS));
//						} else {
//							// 网络连接异常
//							handler.sendMessageDelayed(Message.obtain(handler,
//									DownloadManager.MSG_FINISH, 0), 0);
//						}
//						break;
//					}
//					eventType = webAddress.next();
//				}
//			} catch (ConnectException e) {
//				// TODO: handle exception
//				handler.sendMessageDelayed(
//						Message.obtain(handler, DownloadManager.MSG_FINISH, 0),
//						0);
//			} catch (XmlPullParserException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//				handler.sendMessageDelayed(
//						Message.obtain(handler, DownloadManager.MSG_FINISH, 0),
//						0);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//				handler.sendMessageDelayed(
//						Message.obtain(handler, DownloadManager.MSG_FINISH, 0),
//						0);
//			}
//		}
//
//	}

	/**
	 * @param args
	 */
	public static String[] getCollocation(int id) {
		String[] ret = new String[2];
		String string = "";
		// TODO Auto-generated method stub
		try {
			URI url = new URI(BASE_URL + "query.php?key=" + KEY + "&id=" + id);
			HttpGet get = new HttpGet(url);
			HttpResponse httpResponse = new DefaultHttpClient().execute(get);

			// 判断请求响应状态码，状态码为200表示服务端成功响应了客户端的请求

			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				string = EntityUtils.toString(httpResponse.getEntity());
				System.out.println(string);
			}

		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			JSONArray jsonArray = new JSONArray(string);
			JSONArray item = jsonArray.getJSONArray(0);
			ret[0] = item.getString(6);
			ret[1] = item.getString(7);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

}
